package com.neethi.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
@Table(name="lawyer")
public class Lawyer extends BaseObject{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Long sid;
	private String firstName;
	private String lastName;
	private String displayName;
	private String telephone;
	private String email;
	private Long status;
	private String photo;
	private String specialNote;
	private String registrationId;
	
	private Set<PracticeArea> practiceAreas  = new HashSet<PracticeArea>();
	
	public Lawyer(){
	}
	
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="sid")
	public Long getSid() {
		return sid;
	}

	public void setSid(Long sid) {
		this.sid = sid;
	}
	
	@Column(name="first_name",nullable=false,length=50)
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name="last_name",nullable=false,length=50)
	public String getLastName() {
		return lastName;
	}
	
	@Column(name="display_name",nullable=false,length=100)
	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Column(name="telephone",nullable=false,length=15)
	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	
	@Column(name="email",nullable=false,length=50)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name="status")
	public Long getStatus() {
		return status;
	}

	public void setStatus(Long status) {
		this.status = status;
	}
	
	@Column(name="photo")
	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	@Column(name="special_note",length=256)
	public String getSpecialNote() {
		return specialNote;
	}

	public void setSpecialNote(String specialNote) {
		this.specialNote = specialNote;
	}
	
	@Column(name="registration_id",length=10)
	public String getRegistrationId() {
		return registrationId;
	}

	public void setRegistrationId(String registrationId) {
		this.registrationId = registrationId;
	}
	
	 @ManyToMany(fetch = FetchType.EAGER) 
	 @JoinTable(
	            name="lawyer_parcticearea",
	            joinColumns = { @JoinColumn( name="lawyer_sid") },
	            inverseJoinColumns = @JoinColumn( name="practicearea_sid")
	    )    
	 public Set<PracticeArea> getPracticeAreas() {
	        return practiceAreas;
	 }
	 
	 
	 public void setPracticeAreas(Set<PracticeArea> practiceAreas) {
		this.practiceAreas = practiceAreas;
	}

	/**
	 * Convert user roles to LabelValue objects for convenience.
	 * 
	 * @return a list of LabelValue objects with role information
	 */
	@Transient
	public List<LabelValue> getPracticeAreaList() {
		List<LabelValue> practiceAreasList = new ArrayList<LabelValue>();

		if (this.practiceAreas != null) {
			for (PracticeArea practiceArea : practiceAreas) {
				// convert the practice areas to LabelValue Objects
				practiceAreasList.add(new LabelValue(practiceArea.getName(), practiceArea.getName()));
			}
		}

		return practiceAreasList;
	}
	
	/**
     * Adds a role for the user
     * @param role the fully instantiated role
     */
    public void addPracticeArea(PracticeArea practiceArea) {
        getPracticeAreas().add(practiceArea);
    }

	/**
	 * 
	 */

	@Override
	public boolean equals(Object o) {
		if (this == o) {
            return true;
        }
        if (!(o instanceof Lawyer)) {
            return false;
        }

        final Lawyer lawyer = (Lawyer) o;

        return !(registrationId != null ? !registrationId.equals(lawyer.getRegistrationId()) : lawyer.getRegistrationId() != null);
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
		.append(registrationId)
		.append(email)
		.toHashCode();
	}

	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
		.append("sid", this.sid)
        .append("firstName", this.firstName)
        .append("lastName", this.lastName)
        .append("status", this.status)
        .append("email", this.email)
        .append("registrationId", this.registrationId);
		return sb.toString();
	}

}
